package com.example.shop_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shop_management.entity.Category;
import com.example.shop_management.service.impl.CategoryServiceImpl;
import com.example.shop_management.service.impl.CategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryServiceImpl categoryService;

    @GetMapping("findall")
    public List<Category> findall() {
        QueryWrapper<Category> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.orderByDesc("create_time");
        return categoryService.list(userQueryWrapper);
    }


    @PostMapping("save")
    public boolean insert(@RequestBody Category category){
         return categoryService.saveOrUpdate(category);
    }

    @PostMapping("delete")
    public boolean delete(@RequestBody int id){
        return categoryService.removeById(id);
    }

    @PostMapping("deleltebatch")
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return categoryService.removeByIds(ids);
    }

    //条件查询，按照名字查询
    @GetMapping("selectbycondition")
    public List<Category> selectByCondition(@RequestParam(defaultValue = "",required = false) String cname){
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        if(!"".equals(cname)){
            categoryQueryWrapper.like("cname",cname);
        }
        return categoryService.list(categoryQueryWrapper);
    }


}

